package com.example.asus.homework11.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;


import com.example.asus.homework11.BaseListner;
import com.example.asus.homework11.BaseModel;
import com.example.asus.homework11.R;
import com.example.asus.homework11.Url;
import com.example.asus.homework11.bean.CartBean;
import com.example.asus.homework11.bean.ResultBean;
import com.squareup.picasso.Picasso;

import java.util.List;


public class UserCartAdapter extends RecyclerView.Adapter<UserCartAdapter.MyViewHolder> {

    private List<CartBean> beans;
    private OnItemClickListener onItemClickListener;

    public interface OnItemClickListener{
        void setItem(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener;
    }

    public void setData(List<CartBean> beans){
        this.beans = beans;
        notifyDataSetChanged();
    }
    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new MyViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_cart,viewGroup,false));
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, final int i) {
        final CartBean bean = beans.get(i);
        final int position = i;
        Picasso.get()
                .load(Url.BASEUSRL+bean.getPic())
                .into(myViewHolder.iv_foodlist);
        myViewHolder.tv_foodlist_name.setText(bean.getFoodname());
        myViewHolder.tv_foodlist_price.setText("价格："+bean.getPrice()+"元");
        myViewHolder.btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseModel<ResultBean> model=new BaseModel<>();
                model.callEnqueue(model.service.deleteCart(bean.getItem_id()), new BaseListner<ResultBean>() {
                    @Override
                    public void onResponse(ResultBean resultBean) {
                        if(resultBean!=null&&resultBean.getSuccess().equals("1")){
                            beans.remove(i);
                            notifyDataSetChanged();
                        }
                    }
                    @Override
                    public void onFail(String msg) {

                    }
                });
            }
        });
        if(onItemClickListener!=null){
            myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.setItem(v,position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return beans==null?0:beans.size();
    }

    public String getCartList(){
        StringBuilder list= new StringBuilder();
        for (CartBean bean:beans
             ) {
            list.append(bean.getItem_id()+",");
        }
        list.deleteCharAt(list.length()-1);
        return String.valueOf(list);
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        public View itemView;
        private ImageView iv_foodlist;
        private TextView tv_foodlist_name,tv_foodlist_price;
        private Button btn_delete;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            this.itemView=itemView;
            iv_foodlist=itemView.findViewById(R.id.iv_foodlist);
            tv_foodlist_name=itemView.findViewById(R.id.tv_foodlist_name);
            tv_foodlist_price=itemView.findViewById(R.id.tv_foodlist_price);
            btn_delete=itemView.findViewById(R.id.btn_delete);
        }
    }


}
